clear

*++++++++++++++++++++++++++++++++++++++++++
*FIGURE 1 COMMON TREND IN LOG ASSUMPTION+++
*++++++++++++++++++++++++++++++++++++++++++
use "$data\final.dta"

*only keep pre-treatment period
drop if year > 1992

*generate yearly averages of investment grants per reform cohort
gen first_year = first*1000 + year
format %11.0g first_year
collapse (mean) log_Invzuweis_pc Invzuweis_pc first year, by(first_year)

*plot pre-treatment trends and save graphs
twoway (line log_Invzuweis_pc year if first == 1993 & year < 1993, sort) (line log_Invzuweis_pc year if first == 1994 & year < 1994, sort) (line log_Invzuweis_pc year if first == 1995 & year < 1995, sort) (line log_Invzuweis_pc year if first == 1996 & year < 1996, sort) (line log_Invzuweis_pc year if first == 1997 & year < 1997, sort) (line log_Invzuweis_pc year if first == 1998 & year < 1998, sort), xlabel(1982(1)1992)
graph save Graph "$data\logcommontrend_1992.gph", replace
graph export "$data\logcommontrend_1992.eps", as(eps) preview(on) replace
graph export "$data\logcommontrend_1992.pdf", as(pdf) replace

clear


*++++++++++++++++++++++++++++++++
*FIGURE 2 spatial map of Hesse+++
*++++++++++++++++++++++++++++++++
use "$data\final.dta"
sort code year

*generate auxiliary file that indicates year of first direct elections
encode wahlregel, gen(wahlregel_num)
sort code year
drop if  wahlregel_num==.
by code: gen elect_num=_n
xtset code elect_num
by code: egen mean_wahlregel_num=mean(wahlregel_num)
keep if (wahlregel_num==1 & l.wahlregel_num==2 ) | mean_wahlregel_num==1
rename year first_direct_elect_year
keep code first_direct_elect_year  wahlregel_num elect_num
sort code
save first_direct_elect_year, replace
clear

*combine data on reform cohorts and geographic data
use gemeindedat_Hessen, clear
sort code
merge  n:1 code using first_direct_elect_year
drop _merge
bysort code: egen mean_first_direct_elec=mean(first_direct_elect_year)

* indicate Gemeindefreie Gebiete
replace mean_first_direct_elec=-3 if ISN==65
drop if mean_first_direct_elec==-3

* create the graphs
spmap mean_first_direct_elec  using gemeinde_koor , id(id)  clbreaks( 0 1993 1994 1995 1996 1997 1998 ) clmethod(custom) fcolor(Blues2) legend( title("Year of" "first direct election",  size(*0.5) bexpand    justification(left)))    legend(label( 1 "Excluded" ) label( 2 "1993" ) label(3 "1994") label(4 "1995") label(5 "1996") label(6 "1997") label(7 "1998") )  
graph save Graph "$data\hesse_direct_elect.gph", replace
graph export "$data\hesse_direct_elect.eps", as(eps) preview(on) replace
graph export "$data\hesse_direct_elect.pdf", as(pdf) replace


clear
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*FIGURE 3 EVOLUTION OF TRANSFERS; TWO TYPES OF MUNICIPALITIES++
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
use "$data\final.dta"

*create yearly averages for investment grants across two systems
gen log_invgrpc_dir = log_Invzuweis_pc if direct_elect ==1
gen log_invgrpc_indir = log_Invzuweis_pc if direct_elect ==0
collapse (mean) log_invgrpc_dir log_invgrpc_indir, by(year)

* plot data over time and save graphs
twoway (line log_invgrpc_dir year, sort) (line log_invgrpc_indir year, sort), ytitle(Averages for log of investment grants per capita in Euros) ylabel(, angle(horizontal))xtitle(Year) xtitle(, margin(medsmall)) xlabel(1982(4)2010)
graph export "$data\logoldnewtrend.pdf", as(pdf) replace
graph export "$data\logoldnewtrend.eps", as(eps) preview(off) replace
graph save Graph "$data\logoldnewtrend.gph", replace

clear

*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*TABLE 1 share and number of municipalities with elected/appointed mayor++
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
use "$data\final.dta"

*generate output for table 1
collapse (mean) first, by(code)
tab first

exit
